package demo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/contextDemo2")
public class contextDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        servletContext.setAttribute("count",(int)servletContext.getAttribute("count") +1);
        // 设置响应的内容类型为HTML，字符集为UTF-8
        resp.setContentType("text/html; charset=UTF-8");
        resp.getWriter().println("容器中访问servlet的次数："+servletContext.getAttribute("count"));
    }
}